package com.secondshop.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.secondshop.Common.OsResult;
import com.secondshop.models.Address;
import com.secondshop.models.User;
import com.secondshop.services.AddressService;
import com.secondshop.services.CollectService;
import com.secondshop.services.GoodService;
import com.secondshop.services.ImageService;
import com.secondshop.services.ReviewService;
import com.secondshop.services.TypeService;
import com.secondshop.services.UserService;;

@Controller
@RequestMapping(value = "/uc/user")
public class AddressController {
	private final AddressService addressService;

//	@Autowired
//	private AddressService AddressService;
//	

	@Autowired
	public AddressController(AddressService addressService) {
	this.addressService = addressService;
}


	/**
	 * GET 我的个人中心
	 *
	 * @return
	 */
	@RequestMapping(value = "/portal")
	public String portal() {
		return "/usercenter/user_portal";
	}


	/**
	 * GET 收货地址
	 *
	 * @return
	 */
	
//	  @RequestMapping(value = "/address") public String address(
//	  
//	  @RequestParam(value = "page", required = false, defaultValue = "1") Integer
//	  page,
//	  
//	  @RequestParam(value = "limit", required = false, defaultValue = "3") Integer
//	  limit, HttpServletRequest request, HttpSession session) { User user = (User)
//	  session.getAttribute("user"); List<Address> addresses =
//	  AddressService.pageAddressInfo(page, limit, user.getUserId());
//	  
//		
//		  com.bjpowernode.common.PageInfo info = new
//		  com.bjpowernode.common.PageInfo(page.intValue(), limit.intValue(), "", "");
//		 
//	  info.setTotal((int) AddressService.getL().getTotal());
//	  request.setAttribute("addresses", addresses);
//	  request.setAttribute("pageInfo", info);
//	 
//	  return "/usercenter/user_address"; }
	 


	/**
	 * POST 创建收货地址
	 * 
	 * @return
	 */
	@RequestMapping(value = "/address/insert", method = RequestMethod.POST)
	@ResponseBody
	public Object addressCreate(Address address, HttpSession session) {
		User User = (User) session.getAttribute("user");
		
		address.setUserId(User.getId());
		Integer count = addressService.insertAddress(address);
		if (count == 1) {
	
			return new OsResult(1, "新增收货地址成功");
		} else {
			return new OsResult(0, "新增收货地址失败");
		}

	}
	//显示收货地址
    @RequestMapping(value = "/address/checkout")
    public String checkout(HttpSession session, HttpServletRequest request) {
        User user = (User) session.getAttribute("user");
        // 收货地址
        List<Address> addresses = addressService.listAddress(user.getId());
        request.setAttribute("addresses", addresses);

  
        return "/home/buyCheck";
    }


	/**
	 * PUT 更新收货地址
	 * 
	 * @return
	 */
//	@RequestMapping(value = "/address/update", method = RequestMethod.POST)
//	@ResponseBody
//	public Object addressUpdate(Address address, HttpSession session) {
//		User User = (User) session.getAttribute("user");
//		Address Address = new Address();
//		BeanUtils.copyProperties(address, Address);
//		Address.setUserId(User.getId());
//		int count = addressService.updatetAddress(Address);
//		if (count == 1) {
//			return new OsResult(1, "更新收货地址成功");
//		} else {
//			return new OsResult(0, "更新收货地址失败");
//		}
//	}

	/**
	 * DELETE 删除收货地址
	 * 
	 * @return
	 */
//	@RequestMapping(value = "/address/{addressId}", method = RequestMethod.DELETE)
//	@ResponseBody
//	public Object addressDelete(@PathVariable("addressId") Long addressId, HttpSession session) {
//		Integer count = addressService.deleteByAddressId(addressId);
//		if (count == 1) {
//			return new OsResult(1, "删除收货地址成功");
//		} else {
//			return new OsResult(0, "删除收货地址失败");
//		}
//	}
//
}